<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>

<body>

    <div id="app">

    </div>
    <script>
        //创建Vue对象
        let vue = new Vue({
            el: "#app",
            data: {
                total: 0,
                books: [],
                student: {
                    age: 10
                }
            },
            watch: {
                total(val, oldVal) { //普通的watch监听
                    console.log("total: " + val, oldVal);
                },
                "student.age" (val, oldVal) {
                    console.log("age: " + val, oldVal);
                },
                books: { //深度监听，可监听到对象、数组的变化
                    handler(val, oldVal) {
                        var total = 0;
                        for (var i = 0; i < this.books.length; i++) {
                            var book = this.books[i];
                            total = total + book.num * book.price;
                        }
                        this.total = total;
                    },
                    deep: true //true 深度监听
                }
            }
        });
    </script>
</body>

</html>